package com.wn.class30.service.impl;

import com.wn.class30.entity.Business;
import com.wn.class30.mapper.BusinessDao;
import com.wn.class30.service.BackMyorderShareService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class BackMyorderShareServiceImpl implements BackMyorderShareService {
    @Resource
    private BusinessDao businessDao;
    @Override
    public int selectCount(HashMap map) {

        String businessName = (String) map.get("businessName");
        String businessTel = (String) map.get("businessTel");
        List<Business> allBusi = businessDao.selectByExample(null);
        List<Business> businessList = new ArrayList<>();
        if ((!"".equals(businessName)) && businessName != null &&(!"".equals(businessTel)) && businessTel!=null) {

            for (Business business : allBusi) {
                if(business.getBusinessName().indexOf(businessName)!=-1 &&businessTel.equals(business.getBusinessTel())){
                    businessList.add(business);
                }
            }
            return businessList.size();
        }else if(!"".equals(businessName) && businessName != null&&("".equals(businessTel) || businessTel==null)){
            for (Business business : allBusi) {

                if(business.getBusinessName().indexOf(businessName)!=-1){
                    businessList.add(business);
                }
            }
            return businessList.size();
        }else if(("".equals(businessName) || businessName == null)&&(!"".equals(businessTel) && businessTel!=null)){
            for (Business business : allBusi) {
                if(businessTel.equals(business.getBusinessTel())){
                    businessList.add(business);
                }
            }
            return businessList.size();
        }else{
            return allBusi.size();
        }
    }

    @Override
    public Object select(HashMap map) {
        Integer page = Integer.parseInt((String) map.get("page"));
        Integer limit = Integer.parseInt((String) map.get("limit"));
        String businessName = (String) map.get("businessName");
        String businessTel = (String) map.get("businessTel");
        List<Business> allBusi = businessDao.selectByExample(null);
        List<Business> businessList = new ArrayList<>();
        if (!"".equals(businessName) && businessName != null &&!"".equals(businessTel) && businessTel!=null) {

            for (Business business : allBusi) {
                if(business.getBusinessName().indexOf(businessName)!=-1 &&businessTel.equals(business.getBusinessTel())){
                    businessList.add(business);
                }
            }
            if(((page - 1) * limit+limit)>businessList.size()-1){
                return businessList.subList((page - 1) * limit,businessList.size());
            }else{
                return businessList.subList((page - 1) * limit,limit);
            }

        }else if((!"".equals(businessName) && businessName != null)&&("".equals(businessTel) || businessTel==null)){
            for (Business business : allBusi) {
                if(business.getBusinessName().indexOf(businessName)!=-1){
                    businessList.add(business);
                }
            }
            if(((page - 1) * limit+limit)>businessList.size()-1){
                return businessList.subList((page - 1) * limit,businessList.size());
            }else{
                return businessList.subList((page - 1) * limit,limit);
            }
        }else if(("".equals(businessName) || businessName == null)&&(!"".equals(businessTel) && businessTel!=null)){
            for (Business business : allBusi) {
                if(businessTel.equals(business.getBusinessTel())){
                    businessList.add(business);
                }
            }
            if(((page - 1) * limit+limit)>businessList.size()-1){
                return businessList.subList((page - 1) * limit,businessList.size());
            }else{
                return businessList.subList((page - 1) * limit,limit);
            }
        }else{
            if(((page - 1) * limit+limit)>allBusi.size()-1){
                return allBusi.subList((page - 1) * limit,allBusi.size());
            }else{
                return allBusi.subList((page - 1) * limit,limit);
            }
        }
    }
}
